home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume4 / blachman / part03 < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  31.3 KB

  1. Path: xanth!mcnc!gatech!cwjcc!hal!ncoast!allbery
  2. From: karl@cs.duke.edu (Karl Ramm)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i042: collected awk/sh/csh/etc. goodies (3 of 3)
  5. Message-ID: <8808281546.AA09331@romeo.cs.duke.edu>
  6. Date: 28 Aug 88 15:46:55 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: karl@cs.duke.edu (Karl Ramm)
  9. Lines: 1036
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 42
  13. Submitted-by: "Karl Ramm" <karl@cs.duke.edu>
  14. Archive-name: blachman/Part3
  15.  
  16. From: Nancy Blachman <decvax!decwrl!sun!idi!resonex!nancy@Ucb-Vax.ARPA>
  17. To: net.unix, net.unix-wizards, net.sources
  18. Subject: Actual tricks, shells, csh aliases and the like, 3 of 3
  19. Date: 16 Oct 84 21:24:32 GMT
  20. Organization: Resonex Inc., Sunnyvale, CA
  21.  
  22. > [Know anybody with a GREAT .login or .cshrc?]
  23.  
  24. > I'm interested in collecting the little tricks, shell scripts, awk
  25. > hacks, csh aliases, and such that people have built to make their daily
  26. > life a little easier or more automatic.  Being a fairly new system
  27. > administrator I don't have the big toolbox that years of messing around
  28. > will leave you with.  If you have any hacks you're proud of (or that
  29. > you aren't proud of, but which work anyway), and you're willing to make
  30. > them public, mail them to me.  I'll collect, collate, shuffle, sort,
  31. > munge, judge, select and discard them and then "summarize to the net".
  32.  
  33. This article centers on C programs and awk scripts I received in response to
  34. my solicitation.  The first article concentrates on  aliases, and .cshrc and 
  35. .login files.  The second article in this series focuses shell scripts. 
  36.  
  37. /\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\/
  38. > Nancy Blachman {allegra,hplabs,ihnp4,sun}!resonex!nancy  (408)720 8600 x37 <
  39. /\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\/
  40.  
  41. ::::::::::::::
  42. programs/1
  43. ::::::::::::::
  44. Date: Sat, 15 Sep 84 02:45:08 pdt
  45. From: <hplabs!intelca!t4test!chip>
  46. To: intelca!hplabs!resonex!nancy
  47. Subject: Re: Tricks, shell and awk scripts, csh aliases and the like
  48.  
  49. One thing I've put together is a bunch of scripts which give you a
  50. status line on a DEC type terminal.  The thing is hardwired into VTxxx
  51. type stuff rather than termcap for two reasons.  First, the cursor
  52. save/restore function isn't part of termcap.  Secondly, the set
  53. scrolling region is part of termcap, but it is for VT100 type
  54. terminals.  Since we only have DEC terminals, it doesn't hurt us too
  55. much.
  56.  
  57. Some history:  I used to like to display my current working directory
  58. on my prompt line.  Later I wanted my terminal line there too.  (That
  59. was in case a terminal I was on locked up, I could quickly tell what
  60. terminal to kill.)  Later, when I had four accounts on the machine I
  61. thought it would be nice to display my login name in my prompt.  The
  62. straw which broke the camel's back was that we bought another VAX, and
  63. I wanted to be able to see which machine I was logged on.  As you might
  64. imagine, my prompt now extended about 60 columns across the screen.  My
  65. answer was to take everything out of my prompt except the history
  66. number and stick it at a status line at the bottom of my screen.
  67.  
  68. Below is a shar archive of the status line files.  You do a `source
  69. stat.init' to get stuff initialized.  Then `son' and `soff' (which are
  70. aliased to source the two other scripts) turn the status line on and
  71. off.  These also define a bunch of aliases which help you work with
  72. programs which don't like the status line.  So, I put a `source
  73. /usr/public/stat.init' followed by a `son' in my `.login'.  (I don't
  74. turn it on with my `.cshrc', my shell escapes would then get awfully
  75. sloooooow.)
  76.  
  77. Also, you would need to add `vt100s' and `vt131s' to /etc/termcap.
  78. This is to keep screen stuff (e.g. more, vi, etc.) happy if you run
  79. them with the status line on.  This is what I use:
  80.  
  81.     ################      VT131 with 24th line reserved
  82.     ################ d13s|vt131s|VT131S|vt-131s|pt131s|pt-131s|dec
  83.     vt131s:\
  84.         :li#23:tc=vt131
  85.  
  86. You will definately want to change the terminal identification stuff in
  87. `stat.init'.  We run Eunice, so I use the `grep' to convert the Unix
  88. style terminal line identification to a VMS terminal port.  
  89.  
  90. You can redefine `statin' in the `stat.init' script to put whatever you 
  91. want on the status line.
  92.  
  93. Well...after all this, I hope you use VT1xx/VT2xx terminals.
  94. Otherwise, this won't be real exciting.  (Unless you want to rewrite
  95. `statterm.c')
  96.  
  97. ---
  98.  
  99. Chip Rosenthal, Intel/Santa Clara
  100. { idi|intelca|icalqa|kremvax|qubix|ucscc } ! t4test ! { chip|news }
  101.  
  102. -----  cut here  -----------------------------------------------------------
  103.  
  104. : This is a shar archive.  Extract with sh, not csh.
  105. : This archive contains the following files
  106. :  Makefile stat.init stat.on stat.off statterm.c
  107. : Total number of files: 5
  108. echo x - Makefile [file 1 of 5]
  109. sed 's/^|//' > Makefile << '!-FUNKY-STUFF-!'
  110. |BIN=    /usr/public
  111. |OBJS=    statterm
  112. |
  113. |IOBJS=    $(BIN)/statterm \
  114. |    $(BIN)/stat.init \
  115. |    $(BIN)/stat.on \
  116. |    $(BIN)/stat.off
  117. |
  118. |SOURCES=Makefile \
  119. |    stat.init \
  120. |    stat.on \
  121. |    stat.off \
  122. |    statterm.c
  123. |
  124. |all:        $(OBJS)
  125. |install:    $(IOBJS)
  126. |shar:        stat.shar
  127. |
  128. |statterm:        statterm.c
  129. |            cc -O statterm.c -o statterm
  130. |
  131. |$(BIN)/statterm:    statterm
  132. |            cp $? $@
  133. |            chmod 755 $@
  134. |
  135. |$(BIN)/stat.init:    stat.init
  136. |            cp $? $@
  137. |            chmod 755 $@
  138. |
  139. |$(BIN)/stat.on:        stat.on
  140. |            cp $? $@
  141. |            chmod 755 $@
  142. |
  143. |$(BIN)/stat.off:    stat.off
  144. |            cp $? $@
  145. |            chmod 755 $@
  146. |
  147. |stat.shar:        $(SOURCES)
  148. |            shar $(SOURCES) > $@
  149. |
  150. |
  151. !-FUNKY-STUFF-!
  152. echo x - stat.init [file 2 of 5]
  153. sed 's/^|//' > stat.init << '!-FUNKY-STUFF-!'
  154. |alias a 'alias'
  155. |
  156. |# run statterm to verify terminal can do status line
  157. |statterm
  158. |if $status then
  159. |    # status line not available on this terminal -- see if a
  160. |    # baseterm is defined.  if so, then maybe $TERM is just
  161. |    # messed up from earlier invocation of stat.init.
  162. |    if $?baseterm then
  163. |    # baseterm has been defined -- see if it will work
  164. |    set temp=$TERM
  165. |    setenv TERM $baseterm
  166. |    statterm
  167. |    if $status then
  168. |        # nope -- baseterm will not work either
  169. |        setenv TERM $temp
  170. |        unset temp
  171. |        goto failed
  172. |    else
  173. |        # yes -- baseterm will work
  174. |        echo "changing terminal type from $temp to $TERM"
  175. |        unset temp
  176. |    endif
  177. |    else
  178. |    # no baseterm defined
  179. |    goto failed
  180. |    endif
  181. |endif
  182. |
  183. |# status line will work with this terminal
  184. |set baseterm=$TERM
  185. |set ignoreeof
  186. |
  187. |# find user's system (ick)
  188. |set system=`tr '[A-Z]' '[a-z]' < /usr/include/whoami`
  189. |
  190. |# find user's tty (ick ick ick)
  191. |set temp=`tty`'$'
  192. |set tty=`grep "$temp" /etc/dev.com | sed -e 's/^.*_\(.*\):.*/\1/'`
  193. |unset temp
  194. |
  195. |# setup terminal strings
  196. |set statin="`statterm in '${system}\\!${user}' '(${tty})'`" 
  197. |set statout="`statterm out`" 
  198. |set statoff="`statterm off`"
  199. |echo -n "$statoff"
  200. |
  201. |# setup status commands
  202. |#   son - turn on status mode
  203. |#   soff - turn off status mode
  204. |#   stat - draw status line
  205. |#   nostat - erase status line
  206. |#   termstat - setenv the terminal with status line protected
  207. |#   termnorm - setenv the terminal without a status line
  208. |#   ns - execute a command without a status line
  209. |a son        'source /usr/public/stat.on'
  210. |a soff        'source /usr/public/stat.off'
  211. |a stat        ';'
  212. |a nostat    ';'
  213. |a termstat    ';'
  214. |a termnorm    ';'
  215. |a ns        'nostat ; termnorm ; \!* ; termstat ; stat ; echo ""'
  216. |
  217. |# alias the commands which maintain the status line
  218. |a cd        'chdir \!* ; stat'
  219. |a popd        'popd ; stat'
  220. |a pushd        'pushd \!* ; stat'
  221. |
  222. |# alias the commands which (might) munch the status line
  223. |a clear        'clear \!* ; stat'
  224. |a mail        'mail \!* ; stat'
  225. |a more        'more \!* ; stat'
  226. |
  227. |# alias the commands which should be done with the status line off
  228. |a lo        'ns logout \!*'
  229. |a LO        'ns logout \!*'
  230. |a rn        'ns /local/bin/rn \!*'
  231. |a pn        'ns /local/bin/pn \!*'
  232. |a reply        'ns /local/bin/reply \!*'
  233. |a sus        'ns suspend \!*'
  234. |a vi        'ns /usr/ucb/vi \!*'
  235. |a view        'ns /usr/ucb/view \!*'
  236. |
  237. |# that is all folks.  the status stuff is initialized
  238. |# now all you need is a 'son' to turn it on
  239. |exit 0
  240. |
  241. |failed:
  242. |set baseterm=$TERM
  243. |set temp="status line is not available for terminal type $TERM"
  244. |echo $temp
  245. |set statin=";"
  246. |set statout=";"
  247. |if     $?statoff == 0 then
  248. |    set statoff=";"
  249. |endif
  250. |alias son echo "$temp"
  251. |alias off 'source /usr/public/stat.off'
  252. |unset temp
  253. |soff
  254. |exit 1
  255. |
  256. !-FUNKY-STUFF-!
  257. echo x - stat.on [file 3 of 5]
  258. sed 's/^|//' > stat.on << '!-FUNKY-STUFF-!'
  259. |a stat        'echo -n "${statin}${cwd}${statout}"'
  260. |a nostat    'echo -n "$statoff"'
  261. |a termstat    'setenv TERM ${baseterm}s'
  262. |a termnorm    'setenv TERM $baseterm'
  263. |termstat
  264. |stat
  265. |echo ""
  266. |set statmode
  267. !-FUNKY-STUFF-!
  268. echo x - stat.off [file 4 of 5]
  269. sed 's/^|//' > stat.off << '!-FUNKY-STUFF-!'
  270. |setenv TERM $baseterm
  271. |echo -n "$statoff"
  272. |unset statmode
  273. |a termstat    ';'
  274. |a termnorm    ';'
  275. |a stat ';'
  276. |a nostat ';'
  277. !-FUNKY-STUFF-!
  278. echo x - statterm.c [file 5 of 5]
  279. sed 's/^|//' > statterm.c << '!-FUNKY-STUFF-!'
  280. |/*
  281. | * FILE:    statterm
  282. | * VERSION:    V0.01 [preliminary]
  283. | * DATE:    Mon Aug 27 16:43:13 PDT 1984
  284. | * AUTHOR:      Chip Rosenthal/Intel Corporation
  285. | * ADDRESS:    t4test!chip
  286. | * PACKAGE:     'stat' terminal status line package
  287. | * DESCRIPTION:    produces terminal escape sequences to implement status line
  288. | * COMPILATION: cc -C statterm.c -o statterm
  289. | *
  290. | * REVISION NOTES:
  291. | *
  292. | * V1.00  Original program.
  293. | *
  294. | *
  295. | * USAGE:
  296. | *
  297. | *    statterm 
  298. | *        Verifies that the status line will work with current terminal.
  299. | *        Returns a zero exit status if it will, nonzero if it won't.
  300. | *
  301. | *    statterm in [arg ...]
  302. | *        Creates a string which opens up the status line.  Any
  303. | *        arguments are placed at the beginning of the status
  304. | *        line, and the cursor is left there.
  305. | *
  306. | *    statterm out
  307. | *        Create string which closes the status line and returns
  308. | *        cursor to position it was in prior to the last execution
  309. | *        of a 'statterm in' string.
  310. | *
  311. | *    statterm off
  312. | *        Creates a string which removes the status line.
  313. | *
  314. | *
  315. | *    EXAMPLE:
  316. | *        # this is a cshell script
  317. | *        statterm 
  318. | *        if $status then
  319. | *            echo "status line will not work on this terminal"
  320. | *            exit
  321. | *        endif
  322. | *        echo -n `statterm in $user`
  323. | *        echo -n `pwd`
  324. | *        echo `statterm out`
  325. | *
  326. | *
  327. | * BUGS:       
  328. | *
  329. | *    This program is hardwired into the DEC VT100 terminal escape sequences.
  330. | *    This is done because (unfortunately) 'termcap' doesn't offer a cursor
  331. | *    position save/restore feature.
  332. | *
  333. | */
  334. |
  335. |#include <stdio.h>
  336. |#define strmatch(A,B) (strcmp((A),(B))==0)
  337. |
  338. |/*
  339. | * VT100 Terminal Escape Definitions -- all parameters are strings
  340. | */
  341. |#define ESC           putchar(27);              /* escape character          */
  342. |#define CSI           ESC;putchar('[');         /* command string            */
  343. |#define DECSC           ESC;putchar('7');         /* save cursor position      */
  344. |#define DECRC           ESC;putchar('8');         /* restore cursor position   */
  345. |#define CUP(L,C)      CSI;printf("%s;%sH",L,C); /* set cursor to line/col    */
  346. |#define CUU1          CSI;printf("1A");        /* cursor up one line        */
  347. |#define CUD1          CSI;printf("1B");        /* cursor down one line      */
  348. |#define EL(MODE)      CSI;printf("%sK",MODE);   /* erase line                */
  349. |#define ED(MODE)      CSI;printf("%sJ",MODE);   /* erase display             */
  350. |#define DECSETBM(T,B) CSI;printf("%s;%sr",T,B); /* top/bot of scroll area    */
  351. |/*
  352. | * erasing modes:
  353. | *   mode "0" - from cursor to end
  354. | *   mode "1" - from beginning to cursor
  355. | *   mode "2" - entire
  356. | */
  357. |
  358. |
  359. |main(argc,argv)
  360. |int argc;
  361. |char *argv[];
  362. |{
  363. |    char *term, *getenv();
  364. |    int i;
  365. |
  366. |    if ( argc == 1 ) {
  367. |    term=getenv("TERM");
  368. |    if ( term == NULL )
  369. |        exit(1);
  370. |    if ( strmatch(term,"vt100") || strmatch(term,"vt131") )
  371. |        exit(0);
  372. |    else
  373. |        exit(1);
  374. |    }
  375. |
  376. |    if ( strmatch(argv[1],"in") ) {
  377. |    /*
  378. |     * Save cursor position, go to line 24, print out the arguments, and
  379. |     * erase the rest of the status line.  Cursor remains in status line.
  380. |     */
  381. |    DECSC;
  382. |    CUP("24","1");
  383. |    for ( i=2 ; i<argc ; ++i ) 
  384. |        printf("%s  ",argv[i]);
  385. |    EL("0");
  386. |    } else if ( strmatch(argv[1],"out") ) {
  387. |    /*
  388. |     * Protect status line from scrolling and restore cursor to location 
  389. |     * prior to the last 'in' call.  Move the cursor up and down one line.
  390. |     * This will keep it out of the status line area if it was there when 
  391. |     * 'in' was called.
  392. |     */
  393. |    DECSETBM("1","23");
  394. |    DECRC;
  395. |    CUU1;
  396. |    CUD1;
  397. |    } else if ( strmatch(argv[1],"off") ) {
  398. |    /*
  399. |     * Save the current cursor position, unprotect the status line, erase 
  400. |     * the status line, and restore the cursor position.
  401. |     */
  402. |    DECSC;
  403. |    DECSETBM("1","24");
  404. |    CUP("24","1");
  405. |    EL("2");
  406. |    DECRC;
  407. |    } else
  408. |    exit(1);
  409. |
  410. |}
  411. !-FUNKY-STUFF-!
  412. exit
  413.  
  414.  
  415.  
  416. ::::::::::::::
  417. programs/2
  418. ::::::::::::::
  419. From: ihnp4!mcnc!malloy@ittral
  420. Date: Thu, 13 Sep 84 21:35:18 edt
  421. To: ihnp4!resonex!nancy@mcnc
  422. Subject: Tricks
  423.  
  424. Well it depends upon what you mean. The big problem with having lots of aliases
  425. and such is they start taking up large amounts of memory.  Take it from someone
  426. who knows.  But here's a few things.  Some you no doubt already have, and most
  427. are trivial, but it's better to be complete then to leave anything out.  You
  428. can always just delete this.    == William P. Malloy (ittral!malloy}
  429. ---------------------------------------------------------------------
  430. In a .cshrc, a warning and a work around.  Apparently not known by most people
  431. but obvious to people have who been around.  Our users keep bumping into it
  432. about once a year like clock work.
  433. # reason for setting variables only if a prompt already exists
  434. # If it sets prompt in a non-interactive shell, for instance vi(1)
  435. # firing up a sub-shell to expand shell meta-characters, the set prompt
  436. # will stomp on alot of shell variables used for the expansion (like ~)
  437. #
  438. if ( $?prompt ) then
  439.    set mail=/usr/spool/mail/malloy
  440.    set prompt=\`
  441.    set histchars=",;"
  442. endif
  443. Note the setting of histchars.  A little known, but for me much loved feature
  444. of csh is the ability to change the history characters "!^" from their default
  445. values.  The pair `,;' are easier to reach, don't require shifting, and don't
  446. appear in mail paths.  Typing `mail mcnc\!ihnp4\!resonex\!nancy' gets to be
  447. a pain every time you want to mail someone.
  448.  
  449. alias , 'redo \,* ~/.cmd ~/.cmd1 ; source ~/.cmd1 '
  450.  
  451. This little alias allows you to have command editing in the csh.  It's quite
  452. useful, particularly when you've got a LONG painful command line.  The , is
  453. just the history character and is ! for most people.  The command redo is a
  454. simple C program.
  455. --------------- redo.c  -------------------
  456. #include <stdio.h>
  457.  
  458. /* redo -- outputs a command file (last arg )used to edit and 
  459.    re-ex a command.  Next to last arg is dest file of the command. 
  460.  
  461.    NOTE: if you use a non-standard history character, i.e. not !
  462.    then you must `setenv HISTCHARS $histchars' in your .login
  463.    If you do not `set histchars=",;"' for instance then it will
  464.    automagically default to !    -- 12/2/83 wpm
  465.  
  466.    First arg is the history ref . To use:
  467.     alias , 'redo \,* ~/cmd ~/cmd1 ; source ~/cmd1 '
  468.     , 25    to edit & re-execute ,25
  469.     , , or ,  to edit & re-execute ,,
  470.     , v     to edit & re-execute ,v ,etc. 
  471.     
  472. */
  473.  
  474. main(argc, argv)
  475.    int argc;
  476.    char *argv[];
  477. {
  478.    FILE *fp;
  479.    char *t, *getenv();
  480.  
  481.    fp = fopen(argv[argc-1], "w");
  482.    if ((t = getenv("HISTCHARS")) != NULL)
  483.       t[1] = '\0';
  484.    else
  485.       t = "!";
  486.    fprintf(fp, "echo \"%s", t);
  487.    if ( (argc < 4) || (strcmp(argv[1], t) == 0) )
  488.       fputs("-2", fp);
  489.    else 
  490.       fputs(argv[1], fp);
  491.    fputs(":q\" >! ", fp);
  492.    fputs(argv[argc-2], fp);
  493.    putc('\n', fp);
  494.    fputs("ex +open ", fp);
  495.    fputs(argv[argc-2], fp);
  496.    putc('\n', fp);
  497.    fputs("/usr/local/typein2 < ", fp);
  498.    fputs(argv[argc-2], fp);
  499.    putc('\n', fp);
  500.    fclose(fp);
  501.    exit(0);
  502. }
  503. ------------------ (end of redo.c, begining of typein2.c) --------------------
  504. #include <stdio.h>
  505. #include <sgtty.h>
  506.  
  507. main(argc, argv)
  508.     int argc;
  509.     char **argv;
  510. {
  511.     register char *cp;
  512.     struct sgttyb stb, stb2;
  513.     int pendin = LPENDIN;
  514.     int c,i,j;
  515.     char buff[2];
  516.     char buff2[256];
  517.  
  518.     i=0;
  519.     while ((c=getchar()) != EOF) {
  520.             buff2[i++]=c;
  521.     }
  522.     ioctl(2, TIOCGETP, &stb);
  523.     stb2 = stb;
  524.     stb.sg_flags &= ~ECHO;
  525.     ioctl(2, TIOCSETN, &stb);
  526.     for (j=0; j<i; j++) {
  527.             ioctl(2, TIOCSTI, &buff2[j]);
  528.             putchar(buff2[j]);
  529.     }
  530.     ioctl(2, TIOCSETN, &stb2);
  531.     ioctl(2, TIOCLBIS, &pendin);
  532.     exit(0);
  533. }
  534. ---------------------------- (end of typein2.c) ------------------------
  535. A couple of oldies but still the simplest.  cd and back aliases.
  536. alias back 'set back=$old; set old=$cwd; cd $back; unset back; dirs'
  537. alias cd 'set old=$cwd; chdir \,*; set prompt = "< $cwd:t > "'
  538.  
  539. # this is a useful feature for vi
  540. setenv EXINIT 'map #1 Gi/\<A\>"add@a|map #2 1G\!Gvispell|set ai sw=3'
  541.  
  542. You f2 key will run a file through the spell program, and put the words it
  543. doesn't find in the dictionary at the bottom of the file.  Then typing f1 will
  544. cause it to delete the last line in the file, and search for the string in
  545. the rest of the file.
  546. ------------------------------ (source for vispell) -------------
  547. #! /bin/sh
  548. #
  549. tee /tmp/vis$$
  550. echo SpellingList
  551. spell /tmp/vis$$
  552. rm /tmp/vis$$
  553. ------------------------------- (end of vispell) ------------------
  554. Stuff from our /.cshrc file.  psa lets you see what's going on in the system
  555. and what people are doing.  Useful to see if people are hanging themselves.
  556. alias psa 'ps axu | sed "/getty/d" | sort -f | more'
  557.  
  558. A simple alias to compile C programs (C adm.c expands to cc adm.c -o adm).
  559. alias C 'cc \,:* -o \,^:r'
  560.  
  561. Useful to see what's going on in the system without those !@#$% bells.
  562. alias moremsgs "tr -d '\07' </usr/adm/messages | more"
  563.  
  564. Like fg.
  565. alias pj '%-'
  566.  
  567. A feature we use in our root .cshrc allowing indivual superusers to get their
  568. own enviornment.  Useful if people want their own enviornment anywhere people
  569. share an account.
  570. if ( $user == "malloy" ) then
  571.    source /t/malloy/.root
  572. endif
  573.  
  574. --------------- (here's some things I picked up off the net) --------------
  575. From: ittvax!decvax!harpo!utah-cs!seismo!hao!denelcor!udenva!koala!aburt
  576. Date: Fri Aug 10 09:19:37 1984
  577. Subject: Perversions of source -h and other csh aliases
  578. Newsgroups: net.unix,net.unix-wizards,net.sources
  579.  
  580. For your enjoyment, here are some interesting lunch hour csh aliases that 
  581. I've created.
  582. {this is to slow, my version is a little faster}
  583. My personal favorite is the "history editor" -- allows you to edit
  584. you csh history.  The alias:
  585.  
  586.     alias hed history -h !* > $hed; vi + $hed; source -h $hed
  587.  
  588. will allow you to invoke 'vi' on your current history.  (If, for
  589. instance, you typed in a long, tedious line and put in an extra space,
  590. among other things.  The ^...^...  mechanism to remove it can be quite
  591. tedious; 'hed's is easy.)  Before anyone starts flaming that vi is too
  592. slow, history can do it (even if it's tough to type and you're prone to
  593. more typos doing the history), etc. -- if you don't like it, don't use it.
  594.  
  595. Hed works particularly well if you move the command in question to the
  596. end of the file; then '!!' will execute it after you ZZ from vi.  The
  597. only drawback to this is that it trashes your current event numbers
  598. (they get incremented during the source -h).  By using 'hed 10' you
  599. only edit the last 10 entries in your history.  A temp file, which I
  600. keep in $hed (set to /tmp/hed.$$ from my .login), is used each time for
  601. the history.  Obvious changes to this are to use /tmp/hed.$$ straight
  602. out and add "rm /tmp/hed.$$" to the end of the list.  If you don't like
  603. waiting for the rm, and your /tmp doesn't get cleared out periodically,
  604. put a # in front of the name to make it a disposable file.
  605.  
  606. This may be slower than the vi/emacs mode in the ksh I keep hearing about, but
  607. it does give the functionality.
  608.  
  609. A slight modification to 'hed'...
  610.  
  611.     alias hedf history -h !-0:1 > !-0:2; vi + !-0:2; source -h !-0:2
  612.  
  613. allows you to specify a file to place the history into instead of a temp
  614. file.
  615.  
  616. Another modification yields:
  617.  
  618.     alias heh echo \!* > $hed; vi + $hed; source -h $hed
  619.  
  620. Which lets you edit then add to your history a specific history sequence.
  621. 'Heh' for History edit history.
  622.  
  623. By inserting a 'source \!-0:2' before the source -h in any of the
  624. above has the effect of executing the commands AND placing them on the
  625. history list (from which we may conclude that 'alias so source \!* ; 
  626. source -h \!*' is a useful item: it sources a file then places each line
  627. into your history).
  628.  
  629. On rare occasions you'll have to put extra quotes/escapes around things
  630. when editing the temp file so it gets sourced right.  Particularly around
  631. aliases with raw history substitutions in them (the \!* type of thing).
  632. The easiest is to try it first; if it fails, edit it again.  After all,
  633. two 'hed's are better than one...
  634.  
  635. Regarding the use of !-0:1 instead of !:1 for the first argument to the
  636. current command -- At least one csh is known to accept the former but not
  637. the latter.  (The same csh dumps core (therefore logging you out) on receipt
  638. of the !# history selector.  This is version 1.0 of 4.2BSD csh on a Sun-2.
  639. The 1.1 csh exhibits this behavior:
  640.  
  641.     echo a !#
  642.     a echo a echo
  643.  
  644. adding an extra arg 0 to the end.  Is this common to other cshs out there?)
  645.  
  646. So, for those whose csh's allow !:1, use that where I have !-0:1, etc.
  647.  
  648. Another interesting perversion is an alias'd whereis:
  649.  
  650.     alias wheres ls -l \{'`echo $path | sed "s/ /,/g"`'\}/'`echo \!-0:1 | sed "s/^./[&]/"`'
  651.  
  652. It does an ls -l on every file (passed as arg 1) in any directory on
  653. your current path.  (No aliases, though adding an if at the front
  654. should do the trick.)  The idea is to turn your $path list into a comma
  655. separates list, stick that between {}, and append / and arg 1 to that.
  656. Alas, if the file doesn't exist in one of the directories, you get
  657. errors from ls saying it doesn't exist in a given directory.  So, arg 1
  658. is turned into a pattern, which is allowed to fail; the pattern is,
  659. e.g., foo --> [f]oo.  So the second arg to ls only expands to the
  660. existing files.
  661. ---------------------------- (end of net article) ------------------------
  662. --
  663. Address: William P. Malloy, ITT Telecom, B & CC Engineering Group, Raleigh NC
  664.      {ihnp4!mcnc, burl, ncsu, decvax!ittvax}!ittral!malloy
  665.  
  666.  
  667. ::::::::::::::
  668. awk/1
  669. ::::::::::::::
  670. Date:       Wed, 19 Sep 84 11:44:38 CST
  671. From: David Chase <hplabs!ucbvax!rbbb@rice.ARPA>
  672. Subject:    Re: Tricks, shell and awk scripts, csh aliases and the like
  673. To: resonex!nancy@BERKELEY
  674.  
  675. Here is my favorite awk script.  It turns a list of csrc's from memory
  676. errors on an 11/780 (with MS780C controller) into board and chip position
  677. for certain Mostek memory boards.  This script saved me many headaches,
  678. because the tables in the appendix of the technical manual were hard to read
  679. and contained errors (errors that were noticed as deviations from a pattern
  680. when entering this script).
  681.  
  682. #
  683. # Incredible sleazy awk file to attack memory errors
  684. # Included here is the local configuration, because some decoding
  685. # is board specific
  686. BEGIN    {
  687. # Boards understood by this program
  688.  
  689. #    mk4118 = mostek mk8016 fully populated with mk4116 chips (512k)
  690.     known["mk4118"] = 1
  691.  
  692. #    mk4116 = mostek mk8016 half populated with mk4116 chips (256k)
  693.     known["mk4116"] = 1
  694.  
  695. #    mk8016 = mostek mk8016 fully populated with mk4108 chips (256k)
  696.     known["mk4108"] = 1
  697.  
  698. #    m8210 = DEC 256k board; don't know how to decode this guy (256k)
  699.     known["m8210"] = 0
  700.  
  701. # To add different boards, (i.e., not conforming to this system)
  702. # append to the "pos" map, and make "keys[<array><bank><new board>]"
  703. # reference the appended chip addresses.
  704.  
  705. # Local configuration
  706. # boards is indexed by slot number
  707.     boards["0"] = "m8210"
  708.     boards["1"] = "m8210"
  709.     boards["2"] = "m8210"
  710.     boards["3"] = "m8210"
  711.     boards["4"] = "m8210"
  712.     boards["5"] = "m8210"
  713.     boards["6"] = "mk4116"
  714.     boards["7"] = "mk4116"
  715.     boards["8"] = "mk4116"
  716.     boards["9"] = "mk4116"
  717.     boards["a"] = "mk4116"
  718.     boards["b"] = "mk4116"
  719.     boards["c"] = "mk4118"
  720.     boards["d"] = "mk4118"
  721.     boards["e"] = "mk4118"
  722.     boards["f"] = "mk4118"
  723.     
  724. #    bit in error for a given CRC calculation
  725. #    bits are identified by "u" (upper), "l" (lower), and "c" (check)
  726. #    folowed by the bit number.
  727.  
  728.       bit["01"] = "c0"
  729.       bit["02"] = "c1"
  730.     bit["04"] = "c2"
  731.     bit["08"] = "c3"
  732.     bit["10"] = "c4"
  733.     bit["19"] = "l01"
  734.     bit["1a"] = "l02"
  735.     bit["1c"] = "l04"
  736.     bit["1f"] = "l07"
  737.     bit["20"] = "c5"
  738.     bit["38"] = "l00"
  739.     bit["3b"] = "l03"
  740.     bit["3d"] = "l05"
  741.     bit["3e"] = "l06"
  742.     bit["40"] = "c6"
  743.     bit["49"] = "l09"
  744.     bit["4a"] = "l10"
  745.     bit["4c"] = "l12"
  746.     bit["4f"] = "l15"
  747.     bit["51"] = "l17"
  748.     bit["52"] = "l18"
  749.     bit["54"] = "l20"
  750.     bit["57"] = "l23"
  751.     bit["58"] = "l24"
  752.     bit["5b"] = "l27"
  753.     bit["5d"] = "l29"
  754.     bit["5e"] = "l30"
  755.     bit["68"] = "l08"
  756.     bit["6b"] = "l11"
  757.     bit["6d"] = "l13"
  758.     bit["6e"] = "l14"
  759.     bit["70"] = "l16"
  760.     bit["73"] = "l19"
  761.     bit["75"] = "l21"
  762.     bit["76"] = "l22"
  763.     bit["79"] = "l25"
  764.     bit["7a"] = "l26"
  765.     bit["7c"] = "l28"
  766.     bit["7e"] = "l31"
  767.     bit["80"] = "c7"
  768.     bit["89"] = "u01"
  769.     bit["8a"] = "u02"
  770.     bit["8c"] = "u04"
  771.     bit["8f"] = "u07"
  772.     bit["91"] = "u09"
  773.     bit["92"] = "u10"
  774.     bit["94"] = "u12"
  775.     bit["97"] = "u15"
  776.     bit["98"] = "u16"
  777.     bit["9b"] = "u19"
  778.     bit["9d"] = "u21"
  779.     bit["9e"] = "u22"
  780.     bit["a8"] = "u00"
  781.     bit["ab"] = "u03"
  782.     bit["ad"] = "u05"
  783.     bit["ae"] = "u06"
  784.     bit["b0"] = "u08"
  785.     bit["b3"] = "u11"
  786.     bit["b5"] = "u13"
  787.     bit["b6"] = "u14"
  788.     bit["b9"] = "u17"
  789.     bit["ba"] = "u18"
  790.     bit["bc"] = "u20"
  791.     bit["bf"] = "u23"
  792.     bit["c1"] = "u25"
  793.     bit["c2"] = "u26"
  794.     bit["c4"] = "u28"
  795.     bit["c7"] = "u31"
  796.     bit["e0"] = "u24"
  797.     bit["e3"] = "u27"
  798.     bit["e5"] = "u29"
  799.     bit["e6"] = "u30"
  800.  
  801. #    binary decoding of hex digits
  802.  
  803.     hex["0"]  = "0000"
  804.     hex["1"]  = "0001"
  805.     hex["2"]  = "0010"
  806.     hex["3"]  = "0011"
  807.     hex["4"]  = "0100"
  808.     hex["5"]  = "0101"
  809.     hex["6"]  = "0110"
  810.     hex["7"]  = "0111"
  811.     hex["8"]  = "1000"
  812.     hex["9"]  = "1001"
  813.     hex["a"]  = "1010"
  814.     hex["b"]  = "1011"
  815.     hex["c"]  = "1100"
  816.     hex["d"]  = "1101"
  817.     hex["e"]  = "1110"
  818.     hex["f"]  = "1111"
  819.  
  820. #    chip positions for a given bit, collected across all possible
  821. #    boards.  Each group of 3 letters represents a position.
  822. #    See keys for a better description.
  823.  
  824.     pos["u31"] =  "i01h01g01f01"
  825.     pos["u30"] =  "e01d01c01b01"
  826.     pos["u29"] =  "i02h02g02f02"
  827.     pos["u28"] =  "e02d02c02b02"
  828.     pos["u27"] =  "i03h03g03f03"
  829.     pos["u26"] =  "e03d03c03b03"
  830.     pos["u25"] =  "i04h04g04f04"
  831.     pos["u24"] =  "e04d04c04b04"
  832.     pos["u23"] =  "a01a02a03a04"
  833.     pos["u22"] =  "i05h05g05f05"
  834.     pos["u21"] =  "e05d05c05b05"
  835.     pos["u20"] =  "i06h06g06f06"
  836.     pos["u19"] =  "e06d06c06b06"
  837.     pos["u18"] =  "i07h07g07f07"
  838.     pos["u17"] =  "e07d07c07b07"
  839.     pos["u16"] =  "i08h08g08f08"
  840.     pos["u15"] =  "e08d08c08b08"
  841.     pos["u14"] =  "a05a06a07a08"
  842.     pos["u13"] =  "i09h09g09f09"
  843.     pos["u12"] =  "e09d09c09b09"
  844.     pos["u11"] =  "i10h10g10f10"
  845.     pos["u10"] =  "e10d10c10b10"
  846.     pos["u09"] =  "i11h11g11f11"
  847.     pos["u08"] =  "e11d11c11b11"
  848.     pos["u07"] =  "i12h12g12f12"
  849.     pos["u06"] =  "e12d12c12b12"
  850.     pos["u05"] =  "a09a10a11a12"
  851.     pos["u04"] =  "i13h13g13f13"
  852.     pos["u03"] =  "e13d13c13b13"
  853.     pos["u02"] =  "i14h14g14f14"
  854.     pos["u01"] =  "e14d14c14b14"
  855.     pos["u00"] =  "i15h15g15f15"
  856.     pos["c7"] =   "e15d15c15b15"
  857.     pos["c6"] =   "i16h16g16f16"
  858.     pos["c5"] =   "e16d16c16b16"
  859.     pos["c4"] =   "a13a14a15a16"
  860.     pos["l31"] =  "i17h17g17f17"
  861.     pos["l30"] =  "e17d17c17b17"
  862.     pos["l29"] =  "i18h18g18f18"
  863.     pos["l28"] =  "e18d18c18b18"
  864.     pos["l27"] =  "i19h19g19f19"
  865.     pos["l26"] =  "e19d19c19b19"
  866.     pos["l25"] =  "i20h20g20f20"
  867.     pos["l24"] =  "e20d20c20b20"
  868.     pos["l23"] =  "a17a18a19a20"
  869.     pos["l22"] =  "i21h21g21f21"
  870.     pos["l21"] =  "e21d21c21b21"
  871.     pos["l20"] =  "i22h22g22f22"
  872.     pos["l19"] =  "e22d22c22b22"
  873.     pos["l18"] =  "i23h23g23f23"
  874.     pos["l17"] =  "e23d23c23b23"
  875.     pos["l16"] =  "i24h24g24f24"
  876.     pos["l15"] =  "e24d24c24b24"
  877.     pos["l14"] =  "a21a22a23a24"
  878.     pos["l13"] =  "i25h25g25f25"
  879.     pos["l12"] =  "e25d25c25b25"
  880.     pos["l11"] =  "i26h26g26f26"
  881.     pos["l10"] =  "e26d26c26b26"
  882.     pos["l09"] =  "i27h27g27f27"
  883.     pos["l08"] =  "e27d27c27b27"
  884.     pos["l07"] =  "i28h28g28f28"
  885.     pos["l06"] =  "e28d28c28b28"
  886.     pos["l05"] =  "a25a26a27a28"
  887.     pos["l04"] =  "i29h29g29f29"
  888.     pos["l03"] =  "e29d29c29b29"
  889.     pos["l02"] =  "i30h30g30f30"
  890.     pos["l01"] =  "e30d30c30b30"
  891.     pos["l00"] =  "i31h31g31f31"
  892.     pos["c3"] =   "e31d31c31b31"
  893.     pos["c2"] =   "i32h32g32f32"
  894.     pos["c1"] =   "e32d32c32b32"
  895.     pos["c0"] =   "a29a30a31a32"
  896.  
  897. #    keys is indexed by <board #> <bank> <board type>
  898. #    and yields an index into  a particular pos string
  899. #    for example, board 0, bit 0 on an mk4118 board
  900. #    gives a key of 4.  If the bit in error was c0, then
  901. #    the chip in error is a32 (from the 4th group of 3
  902. #    in pos["c0"].  To change this map, create new keys
  903. #    and (if necessary) append to the pos entries.
  904. #    If it could be more than one chip, then use a multiple
  905. #    digit key (e.g, see the keys for the mk4108 board).
  906.  
  907.     keys["00mk4118"] = "4"
  908.     keys["20mk4118"] = "4"
  909.     keys["40mk4118"] = "4"
  910.     keys["60mk4118"] = "4"
  911.     keys["80mk4118"] = "4"
  912.     keys["a0mk4118"] = "4"
  913.     keys["c0mk4118"] = "4"
  914.     keys["e0mk4118"] = "4"
  915.     keys["01mk4118"] = "2"
  916.     keys["21mk4118"] = "2"
  917.     keys["41mk4118"] = "2"
  918.     keys["61mk4118"] = "2"
  919.     keys["81mk4118"] = "2"
  920.     keys["a1mk4118"] = "2"
  921.     keys["c1mk4118"] = "2"
  922.     keys["e1mk4118"] = "2"
  923.     keys["10mk4118"] = "3"
  924.     keys["30mk4118"] = "3"
  925.     keys["50mk4118"] = "3"
  926.     keys["70mk4118"] = "3"
  927.     keys["90mk4118"] = "3"
  928.     keys["b0mk4118"] = "3"
  929.     keys["d0mk4118"] = "3"
  930.     keys["f0mk4118"] = "3"
  931.     keys["11mk4118"] = "1"
  932.     keys["31mk4118"] = "1"
  933.     keys["51mk4118"] = "1"
  934.     keys["71mk4118"] = "1"
  935.     keys["91mk4118"] = "1"
  936.     keys["b1mk4118"] = "1"
  937.     keys["d1mk4118"] = "1"
  938.     keys["f1mk4118"] = "1"
  939.      
  940.     keys["00mk4116"] = "4"
  941.     keys["20mk4116"] = "4"
  942.     keys["40mk4116"] = "4"
  943.     keys["60mk4116"] = "4"
  944.     keys["80mk4116"] = "4"
  945.     keys["a0mk4116"] = "4"
  946.     keys["c0mk4116"] = "4"
  947.     keys["e0mk4116"] = "4"
  948.     keys["01mk4116"] = "2"
  949.     keys["21mk4116"] = "2"
  950.     keys["41mk4116"] = "2"
  951.     keys["61mk4116"] = "2"
  952.     keys["81mk4116"] = "2"
  953.     keys["a1mk4116"] = "2"
  954.     keys["c1mk4116"] = "2"
  955.     keys["e1mk4116"] = "2"
  956.     keys["10mk4116"] = "4"
  957.     keys["30mk4116"] = "4"
  958.     keys["50mk4116"] = "4"
  959.     keys["70mk4116"] = "4"
  960.     keys["90mk4116"] = "4"
  961.     keys["b0mk4116"] = "4"
  962.     keys["d0mk4116"] = "4"
  963.     keys["f0mk4116"] = "4"
  964.     keys["11mk4116"] = "2"
  965.     keys["31mk4116"] = "2"
  966.     keys["51mk4116"] = "2"
  967.     keys["71mk4116"] = "2"
  968.     keys["91mk4116"] = "2"
  969.     keys["b1mk4116"] = "2"
  970.     keys["d1mk4116"] = "2"
  971.     keys["f1mk4116"] = "2"
  972.      
  973.     keys["00mk4108"] = "34"
  974.     keys["20mk4108"] = "34"
  975.     keys["40mk4108"] = "34"
  976.     keys["60mk4108"] = "34"
  977.     keys["80mk4108"] = "34"
  978.     keys["a0mk4108"] = "34"
  979.     keys["c0mk4108"] = "34"
  980.     keys["e0mk4108"] = "34"
  981.     keys["01mk4108"] = "12"
  982.     keys["21mk4108"] = "12"
  983.     keys["41mk4108"] = "12"
  984.     keys["61mk4108"] = "12"
  985.     keys["81mk4108"] = "12"
  986.     keys["a1mk4108"] = "12"
  987.     keys["c1mk4108"] = "12"
  988.     keys["e1mk4108"] = "12"
  989.     keys["10mk4108"] = "34"
  990.     keys["30mk4108"] = "34"
  991.     keys["50mk4108"] = "34"
  992.     keys["70mk4108"] = "34"
  993.     keys["90mk4108"] = "34"
  994.     keys["b0mk4108"] = "34"
  995.     keys["d0mk4108"] = "34"
  996.     keys["f0mk4108"] = "34"
  997.     keys["11mk4108"] = "12"
  998.     keys["31mk4108"] = "12"
  999.     keys["51mk4108"] = "12"
  1000.     keys["71mk4108"] = "12"
  1001.     keys["91mk4108"] = "12"
  1002.     keys["b1mk4108"] = "12"
  1003.     keys["d1mk4108"] = "12"
  1004.     keys["f1mk4108"] = "12"
  1005.     }
  1006.  
  1007.     {csrc     = $1
  1008.     syndrome = substr (csrc,7,2)
  1009.     board    = substr (csrc,2,1)
  1010.     boardtype = boards[board]
  1011.     bank     = substr (hex [substr (csrc,3,1)],1,1)
  1012.     chips    = bit[syndrome]
  1013.     if (known[boardtype])
  1014.     {  where     = pos[chips]
  1015.        key     = keys[board bank boardtype]
  1016.        sbegin   = 3 * (substr(key,1,1) - 1) + 1
  1017.        thechip  = substr(where,sbegin,3)
  1018.        if (length(key) > 1) 
  1019.        {   sbegin   = 3 * (substr(key,2,1) - 1) + 1
  1020.            thechip  = thechip " and/or " substr(where,sbegin,3)
  1021.        }
  1022. #       printf "\n"
  1023. #       print "csrc =      "    csrc
  1024. #       print "syndrome =  "    syndrome
  1025. #       print "board =     "    board
  1026. #       print "bank =      "    bank
  1027. #       print "chips =     "    chips
  1028. #       print "locations = "    where
  1029. #       print "boardtype = " boardtype
  1030. #       print "key =       " key
  1031. #       print "the chip is "    thechip
  1032.        errors[board "-" thechip]++;
  1033.     }
  1034.     else 
  1035.     {
  1036. #       printf "\n"
  1037. #       print "Board type " boardtype " is unknown"
  1038.        errors["unknown-unknown"]++;
  1039.     }
  1040. }
  1041.     
  1042. END { for (i in errors) {
  1043.     n = split(i,list,"-");
  1044.     printf "%d errors for board %s, chip %s\n",errors[i],list[1],list[2];
  1045.     }
  1046. }
  1047.